#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   75753-ext4.sh
# Version:    1.0
# Date:       2013/11/26
# Author:     yuanhui.shi
# Email:      yuanhui.shi@cs2c.com.cn
# Summary:    04系统兼容性-01文件系统兼容性-03ext4-08快速fsck
# Notes:      ext4执行fsck，效率比ext3高
# Copyright:   China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/11/26
#             -   The first one
# ----------------------------------------------------------------------

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../lib/Echo.sh
source ./lib/cmd.sh
source ../../lib/XmlParse.sh
source ./lib/Partition_parted.sh

function CleanData ()
{
  EchoInfo "卸载分区"
  rm -rf /dir1
  rm -rf /root/test
  EchoInfo "删除分区"
  DeletePartition
  partprobe
  rm /tmp/tmp3 /tmp/tmp4
  EchoInfo "75753-ext4.sh执行完毕"
}

trap "CleanData" EXIT INT

#Create a partition

CreatePartition
# if is 0, get partition failed; else, success.
Partition=$?
[ $Partition -ne 0 -a "x" != "x${Partition}" ]
EchoResult "get test partition: $Partition"


partprobe
# 格式化ext3分区
mkfs.ext3 /dev/sda${Partition}
EchoResult "格式化ext3文件系统分区成功" 

sleep 5

#挂载ext3分区到本地目录
mkdir /dir1
mount  /dev/sda${Partition} /dir1
mount |grep /dev/sda${Partition}
EchoResult "成功挂载ext3分区到本地目录"

EchoInfo "创建一个2G文件"
dd if=/dev/zero of=/root/test  bs=100M count=20

cp /root/test /dir1/

EchoInfo "记录执行fsck时间"
umount /dir1
{ time -p  fsck /dev/sda${Partition};} &>/tmp/tmp3
declare  TIME1=`cat /tmp/tmp3 |grep real|sed 's/^.* //g'`

DeletePartition
#Create a ext4 partition

CreatePartition
# if is 0, get partition failed; else, success.
Partition=$?
[ $Partition -ne 0 -a "x" != "x${Partition}" ]
EchoResult "get test partition: $Partition"

partprobe
mkfs.ext4 /dev/sda${Partition}
mount  /dev/sda${Partition} /dir1

EchoInfo "记录ext4执行fsck时间"
cp /root/test /dir1
umount /dir1

{ time -p  fsck /dev/sda${Partition};} &>/tmp/tmp4
declare  TIME2=`cat /tmp/tmp4 |grep real|sed 's/^.* //g'`

[ `echo "${TIME1} > ${TIME2}" | bc` -eq 1 ]  
EchoResult "ext4文件系统执行fsck所用时间比ext3时间短"

